package com.example.fido2013;

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.ProgressDialog;
import android.content.Context;

import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

public class BroadcastAlertTask extends AsyncTask<String, Integer, String> {

	ProgressDialog progressDialog;
	
	String sessionName;
	String sessionId;
	Context cont;
	String userTypes;
	
	String lat;
	String lon;
	String bombType;
	String bombIsActive;
	String dogID;
	String dogvest;
	
	public BroadcastAlertTask(String sessionName, String sessionId, Context cont, String userTypes, String lat, String lon, String bombType, String bombIsActive, String dogID, String dogvest) {
		this.sessionName = sessionName;
		this.sessionId = sessionId;
		this.cont = cont;
		this.userTypes = userTypes;
		
		this.lat = lat;
		this.lon = lon;
		this.bombType = bombType;
		this.bombIsActive = bombIsActive;
		this.dogID = dogID;
		this.dogvest = dogvest;
	}

	protected void onPreExecute() {
		super.onPreExecute();
		progressDialog = new ProgressDialog(cont);
		progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
		progressDialog.setMessage("Sending broadcast alert...");
		progressDialog.setCancelable(false);
		progressDialog.show();
	}

	protected String doInBackground(String... params) {
		
		HttpClient client = new DefaultHttpClient();
		HttpPost post = new HttpPost("http://dev.m.gatech.edu/d/vkrishnan35/w/fidocpy/c/api/broadcast");
		post.setHeader("Cookie", sessionName + "=" + sessionId);
		
		List<NameValuePair> pairs = new ArrayList<NameValuePair>();
		pairs.add(new BasicNameValuePair("latitude", String.valueOf(lat)));
		pairs.add(new BasicNameValuePair("longitude", String.valueOf(lon)));
		pairs.add(new BasicNameValuePair("dogID", dogID));
		pairs.add(new BasicNameValuePair("dogvest", dogvest));
		pairs.add(new BasicNameValuePair("bombType", bombType));		
		pairs.add(new BasicNameValuePair("bombIsActive", bombIsActive));
		pairs.add(new BasicNameValuePair("userType", userTypes));
		
		try {
			post.setEntity(new UrlEncodedFormEntity(pairs));
		} catch (UnsupportedEncodingException e) {
			e.printStackTrace();
		}
		
		try {
			HttpResponse response = client.execute(post);
		} catch (ClientProtocolException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		System.out.println("broadcast alert task finished");
		
		return "true";
	}

	protected void onPostExecute(String result) {

		super.onPostExecute(result);
		Message message = new Message();
		handler.sendMessage(message);
		progressDialog.dismiss();
	}

	private Handler handler = new Handler() {
		public void handleMessage(Message msg) {
			super.handleMessage(msg);
//			progressDialog.dismiss();
		}
	};

	protected void onCancelled() {
		super.onCancelled();
	}
}
